// Author: wsfuyibing <682805@qq.com>
// Date: 2025-04-27

package config

import (
	"gitee.com/go-libs/config"
	"time"
)

// Maps
// 地图配置.
var Maps MapsConfig

const (
	defaultAmapContentType = "application/json"
	defaultAmapTimeout     = 6
)

type (
	// MapsConfig
	// 地图配置接口.
	MapsConfig interface {
		GetAmapContentType() string
		GetAmapHost() string
		GetAmapKey() string
		GetAmapTimeout() int
		GetAmapTimeoutDuration() time.Duration
		GetType() string
	}

	mapsConfig struct {
		Type string `yaml:"type"`

		// 高德
		AmapContentType     string        `yaml:"amap-content-type"` // 高德API请求格式
		AmapHost            string        `yaml:"amap-host"`         // 高德API地址
		AmapKey             string        `yaml:"amap-key"`          // 高德地图密钥
		AmapTimeout         int           `yaml:"amap-timeout"`      // 高德接口超时
		AmapTimeoutDuration time.Duration `yaml:"-"`                 // 高德接口超时
	}
)

// +---------------------------------------------------------------------------+
// | Interface methods                                                         |
// +---------------------------------------------------------------------------+

func (o *mapsConfig) GetType() string                       { return o.Type }
func (o *mapsConfig) GetAmapContentType() string            { return o.AmapContentType }
func (o *mapsConfig) GetAmapHost() string                   { return o.AmapHost }
func (o *mapsConfig) GetAmapKey() string                    { return o.AmapKey }
func (o *mapsConfig) GetAmapTimeout() int                   { return o.AmapTimeout }
func (o *mapsConfig) GetAmapTimeoutDuration() time.Duration { return o.AmapTimeoutDuration }

// +---------------------------------------------------------------------------+
// | Hook methods                                                              |
// +---------------------------------------------------------------------------+

func (o *mapsConfig) After() {
	if o.AmapContentType == "" {
		o.AmapContentType = defaultAmapContentType
	}
	if o.AmapTimeout == 0 {
		o.AmapTimeout = defaultAmapTimeout
	}
	o.AmapTimeoutDuration = time.Duration(o.AmapTimeout) * time.Second
}

// +---------------------------------------------------------------------------+
// | Access methods                                                            |
// +---------------------------------------------------------------------------+

func (o *mapsConfig) init() *mapsConfig {
	_ = config.Seek("map.yaml", "map.yml").ScanYaml(o)
	return o
}
